/*
* Misc.java
* Copyright (C) 2003
*
* $Id: Misc.java,v 1.8 2005/05/11 21:45:02 cawe Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using Defines = Suake2.UI.Defines;
using VID = Suake2.UI.client.VID;
using FS = Suake2.UI.qcommon.FS;
//UPGRADE_TODO: The type 'java.nio.FloatBuffer' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using FloatBuffer = java.nio.FloatBuffer;
//UPGRADE_TODO: The type 'java.nio.MappedByteBuffer' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using MappedByteBuffer = java.nio.MappedByteBuffer;
//UPGRADE_TODO: The type 'java.nio.channels.FileChannel' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using FileChannel = java.nio.channels.FileChannel;
using GL = net.java.games.jogl.GL;
using BufferUtils = org.lwjgl.BufferUtils;
//UPGRADE_TODO: The 'org.lwjgl.opengl' package was found, but not necessarily converted. References to it may cause compile errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1280'"
using org.lwjgl.opengl;
namespace Suake2.UI.render.lwjgl
{
	
	/// <summary> Misc
	/// 
	/// </summary>
	/// <author>  cwei
	/// </author>
	public abstract class Misc:Mesh
	{
		
		/*
		==================
		R_InitParticleTexture
		==================
		*/
		internal sbyte[][] dottexture = new sbyte[][]{new sbyte[]{0, 0, 0, 0, 0, 0, 0, 0}, new sbyte[]{0, 0, 1, 1, 0, 0, 0, 0}, new sbyte[]{0, 1, 1, 1, 1, 0, 0, 0}, new sbyte[]{0, 1, 1, 1, 1, 0, 0, 0}, new sbyte[]{0, 0, 1, 1, 0, 0, 0, 0}, new sbyte[]{0, 0, 0, 0, 0, 0, 0, 0}, new sbyte[]{0, 0, 0, 0, 0, 0, 0, 0}, new sbyte[]{0, 0, 0, 0, 0, 0, 0, 0}};
		
		internal override void  R_InitParticleTexture()
		{
			int x, y;
			sbyte[] data = new sbyte[8 * 8 * 4];
			
			//
			// particle texture
			//
			for (x = 0; x < 8; x++)
			{
				for (y = 0; y < 8; y++)
				{
					data[y * 32 + x * 4 + 0] = (sbyte) SupportClass.Identity(255);
					data[y * 32 + x * 4 + 1] = (sbyte) SupportClass.Identity(255);
					data[y * 32 + x * 4 + 2] = (sbyte) SupportClass.Identity(255);
					data[y * 32 + x * 4 + 3] = (sbyte) (dottexture[x][y] * 255);
				}
			}
			r_particletexture = GL_LoadPic("***particle***", data, 8, 8, it_sprite, 32);
			
			//
			// also use this for bad textures, but without alpha
			//
			for (x = 0; x < 8; x++)
			{
				for (y = 0; y < 8; y++)
				{
					data[y * 32 + x * 4 + 0] = (sbyte) (dottexture[x & 3][y & 3] * 255);
					data[y * 32 + x * 4 + 1] = 0; // dottexture[x&3][y&3]*255;
					data[y * 32 + x * 4 + 2] = 0; //dottexture[x&3][y&3]*255;
					data[y * 32 + x * 4 + 3] = (sbyte) SupportClass.Identity(255);
				}
			}
			r_notexture = GL_LoadPic("***r_notexture***", data, 8, 8, it_wall, 32);
		}
		
		//	/* 
		//	============================================================================== 
		// 
		//							SCREEN SHOTS 
		// 
		//	============================================================================== 
		//	*/ 
		//
		//	typedef struct _TargaHeader {
		//		unsigned char 	id_length, colormap_type, image_type;
		//		unsigned short	colormap_index, colormap_length;
		//		unsigned char	colormap_size;
		//		unsigned short	x_origin, y_origin, width, height;
		//		unsigned char	pixel_size, attributes;
		//	} TargaHeader;
		
		private const int TGA_HEADER_SIZE = 18;
		
		/* 
		================== 
		GL_ScreenShot_f
		================== 
		*/
		internal override void  GL_ScreenShot_f()
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder(FS.Gamedir() + "/scrshot/jake00.tga");
			FS.CreatePath(sb.ToString());
			System.IO.FileInfo file = new System.IO.FileInfo(sb.ToString());
			// find a valid file name
			int i = 0; int offset = sb.Length - 6;
			bool tmpBool;
			if (System.IO.File.Exists(file.FullName))
				tmpBool = true;
			else
				tmpBool = System.IO.Directory.Exists(file.FullName);
			while (tmpBool && i++ < 100)
			{
				sb[offset] = (char) ((i / 10) + '0');
				sb[offset + 1] = (char) ((i % 10) + '0');
				file = new System.IO.FileInfo(sb.ToString());
				if (System.IO.File.Exists(file.FullName))
					tmpBool = true;
				else
					tmpBool = System.IO.Directory.Exists(file.FullName);
			}
			if (i == 100)
			{
				VID.Printf(Defines.PRINT_ALL, "Clean up your screenshots\n");
				return ;
			}
			
			try
			{
				//UPGRADE_TODO: Class 'java.io.RandomAccessFile' was converted to 'System.IO.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioRandomAccessFile'"
				System.IO.FileStream out_Renamed = SupportClass.RandomAccessFileSupport.CreateRandomAccessFile(file, "rw");
				FileChannel ch = out_Renamed.getChannel();
				int fileLength = TGA_HEADER_SIZE + vid.width * vid.height * 3;
				out_Renamed.SetLength(fileLength);
				MappedByteBuffer image = ch.map(FileChannel.MapMode.READ_WRITE, 0, fileLength);
				
				// write the TGA header
				image.put(0, (sbyte) 0).put(1, (sbyte) 0);
				image.put(2, (sbyte) 2); // uncompressed type
				image.put(12, (sbyte) (vid.width & 0xFF)); // vid.width
				image.put(13, (sbyte) (vid.width >> 8)); // vid.width
				image.put(14, (sbyte) (vid.height & 0xFF)); // vid.height
				image.put(15, (sbyte) (vid.height >> 8)); // vid.height
				image.put(16, (sbyte) 24); // pixel size
				
				// go to image data position
				image.position(TGA_HEADER_SIZE);
				
				
				// change pixel alignment for reading
				if (vid.width % 4 != 0)
				{
					gl.glPixelStorei(net.java.games.jogl.GL_Fields.GL_PACK_ALIGNMENT, 1);
				}
				
				// OpenGL 1.2+ supports the GL_BGR color format
				// check the GL_VERSION to use the TARGA BGR order if possible
				// e.g.: 1.5.2 NVIDIA 66.29
				if (gl_config.OpenGLVersion >= 1.2f)
				{
					// read the BGR values into the image buffer
					gl.glReadPixels(0, 0, vid.width, vid.height, GL12.GL_BGR, net.java.games.jogl.GL_Fields.GL_UNSIGNED_BYTE, image);
				}
				else
				{
					// read the RGB values into the image buffer
					gl.glReadPixels(0, 0, vid.width, vid.height, GL11.GL_RGB, net.java.games.jogl.GL_Fields.GL_UNSIGNED_BYTE, image);
					// flip RGB to BGR
					sbyte tmp;
					for (i = TGA_HEADER_SIZE; i < fileLength; i += 3)
					{
						tmp = image.get_Renamed(i);
						image.put(i, image.get_Renamed(i + 2));
						image.put(i + 2, tmp);
					}
				}
				// reset to default alignment
				gl.glPixelStorei(net.java.games.jogl.GL_Fields.GL_PACK_ALIGNMENT, 4);
				// close the file channel
				ch.close();
			}
			catch (System.IO.IOException e)
			{
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
				VID.Printf(Defines.PRINT_ALL, e.Message + '\n');
			}
			
			VID.Printf(Defines.PRINT_ALL, "Wrote " + file + '\n');
		}
		
		/*
		** GL_Strings_f
		*/
		internal override void  GL_Strings_f()
		{
			VID.Printf(Defines.PRINT_ALL, "GL_VENDOR: " + gl_config.vendor_string + '\n');
			VID.Printf(Defines.PRINT_ALL, "GL_RENDERER: " + gl_config.renderer_string + '\n');
			VID.Printf(Defines.PRINT_ALL, "GL_VERSION: " + gl_config.version_string + '\n');
			VID.Printf(Defines.PRINT_ALL, "GL_EXTENSIONS: " + gl_config.extensions_string + '\n');
		}
		
		/*
		** GL_SetDefaultState
		*/
		internal override void  GL_SetDefaultState()
		{
			gl.glClearColor(1f, 0f, 0.5f, 0.5f); // original quake2
			//gl.glClearColor(0, 0, 0, 0); // replaced with black
			gl.glCullFace(GL11.GL_FRONT);
			gl.glEnable(GL11.GL_TEXTURE_2D);
			
			gl.glEnable(GL11.GL_ALPHA_TEST);
			gl.glAlphaFunc(GL11.GL_GREATER, 0.666f);
			
			gl.glDisable(GL11.GL_DEPTH_TEST);
			gl.glDisable(GL11.GL_CULL_FACE);
			gl.glDisable(GL11.GL_BLEND);
			
			gl.glColor4f(1, 1, 1, 1);
			
			gl.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
			gl.glShadeModel(GL11.GL_FLAT);
			
			GL_TextureMode(gl_texturemode.string_Renamed);
			GL_TextureAlphaMode(gl_texturealphamode.string_Renamed);
			GL_TextureSolidMode(gl_texturesolidmode.string_Renamed);
			
			gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, gl_filter_min);
			gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, gl_filter_max);
			
			gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
			gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
			
			gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
			
			GL_TexEnv(GL11.GL_REPLACE);
			
			if (qglPointParameterfEXT)
			{
				// float[] attenuations = { gl_particle_att_a.value, gl_particle_att_b.value, gl_particle_att_c.value };
				FloatBuffer att_buffer = BufferUtils.createFloatBuffer(4);
				att_buffer.put(0, gl_particle_att_a.value_Renamed);
				att_buffer.put(1, gl_particle_att_b.value_Renamed);
				att_buffer.put(2, gl_particle_att_c.value_Renamed);
				
				gl.glEnable(GL11.GL_POINT_SMOOTH);
				gl.glPointParameterfEXT(EXTPointParameters.GL_POINT_SIZE_MIN_EXT, gl_particle_min_size.value_Renamed);
				gl.glPointParameterfEXT(EXTPointParameters.GL_POINT_SIZE_MAX_EXT, gl_particle_max_size.value_Renamed);
				gl.glPointParameterEXT(EXTPointParameters.GL_DISTANCE_ATTENUATION_EXT, att_buffer);
			}
			
			if (qglColorTableEXT && gl_ext_palettedtexture.value_Renamed != 0.0f)
			{
				gl.glEnable(EXTSharedTexturePalette.GL_SHARED_TEXTURE_PALETTE_EXT);
				
				GL_SetTexturePalette(d_8to24table);
			}
			
			GL_UpdateSwapInterval();
			
			/*
			* vertex array extension
			*/
			gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
			gl.glClientActiveTextureARB(GL_TEXTURE0);
			gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		}
		
		internal override void  GL_UpdateSwapInterval()
		{
			if (gl_swapinterval.modified)
			{
				gl_swapinterval.modified = false;
				if (!gl_state.stereo_enabled)
				{
					if (qwglSwapIntervalEXT)
					{
						// ((WGL)gl).wglSwapIntervalEXT((int)gl_swapinterval.value);
					}
				}
			}
		}
	}
}